using System;
using System.Linq;
using System.Text;
using DevExpress.Xpo;
using DevExpress.ExpressApp;
using System.ComponentModel;
using DevExpress.ExpressApp.DC;
using DevExpress.Data.Filtering;
using DevExpress.Persistent.Base;
using System.Collections.Generic;
using DevExpress.ExpressApp.Model;
using DevExpress.Persistent.BaseImpl;
using DevExpress.Persistent.Validation;
using DevExpress.ExpressApp.ConditionalAppearance;
using DomainComponents.Common;

namespace DomainComponents.XCRM
{
    public enum LeadStatus
    {
        None = 0,
        Lost = 1,
        [DevExpress.Xpo.DisplayName("Cannot Contact")]
        CannotContact = 2,
        [DevExpress.Xpo.DisplayName("No Longer Interested")]
        NoLongerInterested = 3,
        Canceled = 4,
        Qualified = 5
    }

    public enum LeadSource
    {
        ColdCall,
        ExistingCustomer,
        SelfGenerated,
        EmployeeReferral,
        Partner,
        PublicRelations,
        Seminar,
        TradeShow,
        Web,
        WordOfMouth,
        Advertisement,
        Other
    }

    public class LeadValidationRules
    {
        public const string TopicIsRequired = "TopicIsRequired";
        public const string CompanyNameIsRequired = "CompanyNameIsRequired";
        public const string LastNameIsRequired = "LeadLastNameIsRequired";
    }

    [DomainComponent]
    [NonPersistentDc]
    public interface ILeadTarget
    {
        [VisibleInListView(false)]
        ILead SourceLead { get; set; }
    }

    [DomainComponent]
    [VisibleInReports]
    [ImageName("BO_Lead")]
    [Appearance("Disable ILead by Status", TargetItems = "*", Criteria = "Status != 'None'", Enabled = false)]
    [XafDefaultProperty("Name")]
    [XafDisplayName("Lead")]
    //[ListViewFilter("Lead_OpenLeads", "[Status] = 'None'", "Open Leads", true, Index = 0)]
    //[ListViewFilter("Lead_ClosedLeads", "[Status] != 'None'", "Closed Leads", Index = 2)]
    public interface ILead
    {
        [Calculated("isnull(FirstName, '') + ' ' + isnull(LastName, '')")]
        string Name { get; }

        [RuleRequiredField(LeadValidationRules.TopicIsRequired, DefaultContexts.Save)]
        [VisibleInLookupListView(true)]
        string Topic { get; set; }

        LeadStatus Status { get; set; }

        DateTime CreatedOn { get; set; }

        [RuleRequiredField(LeadValidationRules.CompanyNameIsRequired, DefaultContexts.Save)]
        [VisibleInListView(false)]
        string CompanyName { get; set; }

        [RuleRequiredField(LeadValidationRules.LastNameIsRequired, DefaultContexts.Save)]
        [VisibleInListView(false)]
        string LastName { get; set; }

        [VisibleInListView(false)]
        string FirstName { get; set; }

        [VisibleInListView(false)]
        LeadSource LeadSource { get; set; }

        [VisibleInListView(false)]
        IDCUser Owner { get; set; }

        [VisibleInListView(false)]
        string JobTitle { get; set; }

        [VisibleInListView(false)]
        string WebSite { get; set; }

        [VisibleInListView(false)]
        decimal AnnualRevenue { get; set; }

        [VisibleInListView(false)]
        string NumberOfEmployees { get; set; }

        [VisibleInListView(false)]
        string SICCode { get; set; }

        [VisibleInListView(false)]
        PreferredContactMethod PreferredContactMethod { get; set; }

        [Action(PredefinedCategory.View, Caption = "Disqualify Lead...", AutoCommit = true,
            TargetObjectsCriteria = "Status = 'None'",
            SelectionDependencyType = MethodActionSelectionDependencyType.RequireSingleObject)]
        void Disqualify(DisqualifyLeadParameters paremeters);

        [Action(PredefinedCategory.View, Caption = "Reactivate Lead...", AutoCommit = true,
            TargetObjectsCriteria = "Status != 'None'",
            SelectionDependencyType = MethodActionSelectionDependencyType.RequireSingleObject)]
        void Reactivate();

        void CopyTo(ILeadTarget leadTarget);
    }

    [DomainComponent]
    [NonPersistentDc]
    public interface IGenericEmail
    {
        [VisibleInListView(false)]
        string Email { get; set; }
        void Copy(IGenericEmail target);
    }

    [DomainLogic(typeof(IGenericEmail))]
    public class GenericEmailLogic
    {
        public static void Copy(IGenericEmail source, IGenericEmail target)
        {
            if (target != null)
            {
                target.Email = source.Email;
            }
        }
    }

    [DomainLogic(typeof(ILead))]
    public class LeadLogic
    {
        public static void AfterConstruction(ILead lead, IObjectSpace os)
        {
            lead.CreatedOn = DateTime.Now;
        }
        public static void Reactivate(ILead lead)
        {
            lead.Status = LeadStatus.None;
        }
        public static string Get_DisplayName(ILead lead)
        {
            return lead.Name;
        }
        public static void CopyTo(ILead lead, ILeadTarget leadTarget)
        {
            if (leadTarget != null)
            {
                ((ILeadTarget)leadTarget).SourceLead = lead;
            }
        }
    }


    //[DomainComponent]
    //[NonPersistentDc]
    //public interface IGenericEmail
    //{
    //    [VisibleInListView(false)]
    //    string Email { get; set; }
    //    void Copy(IGenericEmail target);
    //}


    //[DomainComponent]
    //[NonPersistentDc]
    //public interface ILeadTarget
    //{
    //    [VisibleInListView(false)]
    //    ILead SourceLead { get; set; }
    //}

    //[DefaultClassOptions]
    ////[ImageName("BO_Contact")]
    ////[DefaultProperty("DisplayMemberNameForLookupEditorsOfThisType")]
    ////[DefaultListViewOptions(MasterDetailMode.ListViewOnly, false, NewItemRowPosition.None)]
    ////[Persistent("DatabaseTableName")]
    //// Specify more UI options using a declarative approach (http://documentation.devexpress.com/#Xaf/CustomDocument2701).
    //public class ILead : BaseObject
    //{ // Inherit from a different class to provide a custom primary key, concurrency and deletion behavior, etc. (http://documentation.devexpress.com/#Xaf/CustomDocument3146).
    //    public ILead(Session session)
    //        : base(session)
    //    {
    //    }
    //    public override void AfterConstruction()
    //    {
    //        base.AfterConstruction();
    //        // Place your initialization code here (http://documentation.devexpress.com/#Xaf/CustomDocument2834).
    //    }
    //    //private string _PersistentProperty;
    //    //[XafDisplayName("My display name"), ToolTip("My hint message")]
    //    //[ModelDefault("EditMask", "(000)-00"), Index(0), VisibleInListView(false)]
    //    //[Persistent("DatabaseColumnName"), RuleRequiredField(DefaultContexts.Save)]
    //    //public string PersistentProperty {
    //    //    get { return _PersistentProperty; }
    //    //    set { SetPropertyValue("PersistentProperty", ref _PersistentProperty, value); }
    //    //}

    //    //[Action(Caption = "My UI Action", ConfirmationMessage = "Are you sure?", ImageName = "Attention", AutoCommit = true)]
    //    //public void ActionMethod() {
    //    //    // Trigger a custom business logic for the current record in the UI (http://documentation.devexpress.com/#Xaf/CustomDocument2619).
    //    //    this.PersistentProperty = "Paid";
    //    //}
    //}
}
